/**
 * Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
 * https://github.com/ant-design/ant-design-pro-layout/blob/master/README.zh-CN.md
 */
import ProLayout, { SettingDrawer,  DefaultFooter,Settings} from '@ant-design/pro-layout';
import React, { useEffect } from 'react';
import Link from 'umi/link';
import { connect } from 'dva';
import { BackTop,Icon } from 'antd';
import { formatMessage } from 'umi-plugin-react/locale';
import Authorized from '@/utils/Authorized';
import RightContent from '@/components/GlobalHeader/RightContent';
import { isAntDesignPro } from '@/utils/utils';
import logo from '../assets/logo.svg';
import request from '@/utils/request';


const menuDataRender = menuList =>
  menuList.map(item => {
    const localItem = {
      ...item,
      children: item.children ? menuDataRender(item.children) : [],
    };
    return Authorized.check(item.authority, localItem, null);
  });

// const footerRender = (_, defaultDom) => {
//   if (!isAntDesignPro()) {
//     return defaultDom;
//   }


  const footerRender = (_, defaultDom) => {
    if (!isAntDesignPro()) {
      return (
        <DefaultFooter
          links={[
            {
              key: '洲博通 官网',
              title: '洲博通 官网',
              href: 'https://www.zbtservices.com/',
              blankTarget: true,
            },
            {
              key: 'apple',
              title: <Icon type="apple" spin theme="filled" />,
              href: 'https://www.zbtservices.com/',
              blankTarget: true,
            },
            {
              key: '洲博通 外综',
              title: '洲博通 外综',
              href: 'http://www.vmaotong.com:8083',
              blankTarget: true,
            },
          ]}
          copyright="2014-2019 Kim 版权所有 ICP证：粤ICP备16065984号-2"
        />
      );
    }


  return (
    <>
      {defaultDom}
      <div
        style={{
          padding: '0px 24px 24px',
          textAlign: 'center',
        }}
      >
        <a href="https://www.netlify.com" target="_blank" rel="noopener noreferrer">
          <img
            src="https://www.netlify.com/img/global/badges/netlify-color-bg.svg"
            width="82px"
            alt="netlify logo"
          />
        </a>
      </div>
    </>
  );
};

const BasicLayout = props => {
  const { dispatch, children, settings, menu } = props;
  useEffect(() => {
    if (dispatch) {
      dispatch({ type: 'user/fetchCurrent', });
    }
    dispatch({
      type: 'settings/getSetting',
    });
    dispatch({
      type: 'menu/fetch',
    });
  }, []);

  const handleMenuCollapse = payload =>
    dispatch &&
    dispatch({
      type: 'global/changeLayoutCollapsed',
      payload,
    });

  return (
    <>
      <ProLayout
        logo={logo}
        // logo={
        //   <div style={{ verticalAlign: 'middle', fontSize: 26, color: '#fff' }}>
        //     <img src={logo} />
        //     {!props.collapsed?<span style={{ marginLeft: 20 }}>洲博通</span>:null}
        //   </div>
        // }
        onCollapse={handleMenuCollapse}
        menuItemRender={(menuItemProps, defaultDom) => {
          if (menuItemProps.isUrl) {
            return defaultDom;
          } // menuItemRender 	(itemProps: MenuDataItem) => ReactNode

          return <Link to={menuItemProps.path}>{defaultDom}</Link>;
        }}
        breadcrumbRender={(routers = []) => [
          {
            path: '/',
            breadcrumbName: formatMessage({
              id: 'menu.home',
              defaultMessage: 'Home',
            }),
          },
          ...routers,
        ]}
        itemRender={(route, params, routes, paths) => {
          const first = routes.indexOf(route) === 0;
          return first ? (
            <Link to={paths.join('/')}>{route.breadcrumbName}</Link>
          ) : (
              <span>{route.breadcrumbName}</span>
            );
        }}
        footerRender={footerRender} //自定义页脚的 render 方法 	(props: BasicLayoutProps) => ReactNode
        menuDataRender={menuList =>
          // props.menu.list.map(item => {
            menuList.map(item => {
            const localItem = {
              ...item,
              children: item.children ? menuDataRender(item.children) : [],
            };
            return Authorized.check(item.authority, localItem, null);
          })
        }
        formatMessage={formatMessage}
        rightContentRender={rightProps => <RightContent {...rightProps} />} //自定义头右部的 render 方法 (props: HeaderViewProps) => ReactNode
        // siderWidth={256}
        // locale="en-US" //当前layout 的语言设置zh-CN' | 'zh-TW' | 'en-US'
        // menuRender={()=>{"自定义菜单的 render 方法"}} 	(props: HeaderViewProps) => ReactNode
        // pageTitleRender={()=>"自定义页面标题的显示方法"} (props: BasicLayoutProps) => ReactNode
        // headerRender={()=>"自定义头的 render 方法"}    (props: BasicLayoutProps) => ReactNode
        // rightContentRender={()=>"自定义头的 render 方法"}  	(props: HeaderViewProps) => ReactNode
        // collapsed={false}
        {...props}
        {...settings}
      >
        {children}
        <BackTop visibilityHeight={200} />
      </ProLayout>
      <SettingDrawer
        settings={settings}
        onSettingChange={config =>
          dispatch({
            type: 'settings/changeSetting',
            payload: config,
          })
        }
      />
    </>
  );
}

export default connect(({ global, settings, menu }) => ({
  collapsed: global.collapsed,
  settings,
  menu,
}))(BasicLayout);


// <BackTop />
// 参数	                    说明	类型	默认值	版本
// target	                  设置需要监听其滚动事件的元素，值为一个返回对应 DOM 元素的函数	Function	() => window
// visibilityHeight	        滚动高度达到此参数值才出现 BackTop	number	400
// onClick	                点击按钮的回调函数	Function	-
